

package cell

import (
	"github.com/treelab/updatecolumn/models"
)

// InstanceType of cell
func InstanceType() int32 {
	return models.MultiSelect
}


  cellValueToSelect({ column, cell }: IColumnAndCell) {
    const cellOptions = cell.hasColumnOption.get();
    const option = _.find(
      column.hasColumnOption.get(),
      columnOption => !!cellOptions.find(cellOption => cellOption.uid === columnOption.uid),
    );
    return option;
  }

  cellValueToCollaborator({
    cell,
    column,
    userGroup,
    columnConfig,
  }: {
    cell: Cell;
    column: Column;
    userGroup: any;
    columnConfig: any;
  }) {
    const userGroups = userGroup?.groups || [];
    const groups: any[] = [];
    let textArray = this.getSelectedOption({ column, cell });
    const toColumnConfig = columnConfig as CollaboratorColumnConfig;
    if (!toColumnConfig || !toColumnConfig.useMultiple) {
      textArray = textArray?.slice(0, 1);
    }
    textArray?.forEach((option: any) => {
      const text = option.name;
      const find = this.findCollaboratorGroupAndUser(userGroups, text, ['name', 'phoneNumber', 'email']);
      if (find) {
        groups.push(find);
      }
    });
    return _.uniq(groups);
  }

  setCellValue({ cell, action, isNewCell }: ICCAction, o: CommonOptions) {
    const { type: actionType } = action;
    const multiSelectOptions = (action.value as MultiSelectCellValue).selectedOptions!;
    if (actionType === UpdateAction.SET_VALUE || actionType === UpdateAction.ADD_VALUE) {
      if (actionType === UpdateAction.SET_VALUE && cell.uid && !isNewCell) {
        o.delQuads.push(...this.dgraphHelperService.getRemoveEdgeQuads(cell.uid!, Relation.HAS_COLUMN_OPTION));
        cell.hasColumnOption.deleteAll();
      }

      _.forEach(multiSelectOptions, multiSelectOption => {
        cell.hasColumnOption
          .withFacet(new OrderFacet({ order: cell.hasColumnOption.get().length }))
          .add(multiSelectOption);
      });
    } else if (actionType === UpdateAction.REMOVE_VALUE) {
      if (!cell.uid) {
        throw new CellNotExistError('');
      }

      multiSelectOptions.forEach(multiSelectOption => {
        if (!isNewCell) {
          o.delQuads.push(
            ...this.dgraphHelperService.getRemoveEdgeQuads(
              cell.uid!,
              Relation.HAS_COLUMN_OPTION,
              multiSelectOption.uid,
            ),
          );
        }
      });
    }
    delete (action.value as MultiSelectCellValue).selectedOptions;
    return this;
  }

  getCellValue(cell: CellData) {
    return _.map(cell.options, (option: any) => option.id);
  }

  protected getAction(command: UpdateColumn_v2, fromColumn: Column, cell: Cell, userGroup?: any) {
    const { column, columnConfig } = command;
    const { type: toType } = columnConfig;
    const { columnType: fromType } = fromColumn;

    const action: any = {
      type: UpdateAction.SET_VALUE,
      value: { ...command.columnConfig, type: toType },
    };
    delete action.value.options;

    const value = action.value as MultiSelectCellValue;
    const fromCellHandle = this.instanceFactory.getCellInstance(fromType as ColumnType);
    const selectOptions = fromCellHandle.cellValueToMultiSelect({ column, cell, userGroup });
    if (selectOptions) {
      value.selectedOptionIds = (selectOptions || []).map(
        selectOption => selectOption?.uid ?? this.getNodeTempId(selectOption),
      );
      value.selectedOptions = selectOptions;
    } else {
      delete value.selectedOptions;
    }
    return action;
  }

  protected addCellUpdateEvent(command: UpdateCell_v2, cell: Cell, o: CommonOptions) {
    const { workspaceId, coreId, tableId, action, _metadata } = command!;
    const cellId = cell.uid ?? this.getNodeTempId(cell);
    o!.events.addEvents(
      new CellUpdated(workspaceId, coreId, tableId, cell.columnId, cell.rowId, cellId, action, _metadata),
      ['cellId', 'selectedOptionIds'],
    );
  }

